/**
 * 神州信息(2012)
 */
package net.abbp.report.export;

import net.abbp.report.model.Cell;
import net.abbp.report.model.ReportModel;
import net.abbp.report.model.cell.style.HorizentalAlignment;
import net.abbp.report.model.cell.style.Style;
import net.abbp.report.model.cell.style.VerticalAlignment;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

/**
 * 子模型导出
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        <pre>
 * 历史：
 *      建立: Jul 31, 2012 lexloo
 * </pre>
 */
public class ReportModelValueExport implements IValueExport {
    @Override
    public PdfPCell cell2PdfCell(Cell cell, ReportModel model) {
        ValueExportManager vem = ValueExportManager.INSTANCE;

        PdfPTable table = null;
        Object value = cell.getValue();
        if (value != null) {
            ReportModel vm = (ReportModel) value;
            table = new PdfPTable(vm.getColumnCount());
            float[] widthArr = new float[vm.getColumnCount()];
            for (int i = 0; i < vm.getColumnCount(); i++) {
                widthArr[i] = vm.getColumnWidth(i);
            }
            try {
                table.setWidths(widthArr);
            } catch (DocumentException e) {
                e.printStackTrace();
            }

            for (int row = 0; row < vm.getRowCount(); row++) {
                for (int column = 0; column < vm.getColumnCount(); column++) {
                    Cell c = vm.getCell(column, row);

                    IValueExport export;
                    if (c == Cell.EMPTY_CELL) {
                    } else {
                        if (c.getValue() == null) {
                            export = vem.getValueExport(Object.class);
                        } else {
                            export = vem.getValueExport(c.getValue().getClass());
                        }

                        PdfPCell pc = export.cell2PdfCell(c, vm);
                        if (c.isMerged()) {
                            pc.setColspan(c.getColumnSpan());
                            pc.setRowspan(c.getRowSpan());
                        }
                        table.addCell(pc);
                    }
                }
            }
        }

        PdfPCell rtn = new PdfPCell(table);
        rtn.setFixedHeight(model.getRowHeight(cell.getRow()));
        Style style = cell.getStyle();
        HorizentalAlignment hAlign = style.getHorizentalAlignment();
        if (hAlign == HorizentalAlignment.Left) {
            rtn.setHorizontalAlignment(Element.ALIGN_LEFT);
        } else if (hAlign == HorizentalAlignment.Right) {
            rtn.setHorizontalAlignment(Element.ALIGN_RIGHT);
        } else {
            rtn.setHorizontalAlignment(Element.ALIGN_CENTER);
        }
        rtn.setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中
        
        VerticalAlignment vAlign = style.getVerticalAlignment();
        if (vAlign == VerticalAlignment.Top) {
            rtn.setVerticalAlignment(Element.ALIGN_TOP);
        } else if (vAlign == VerticalAlignment.Bottom) {
            rtn.setVerticalAlignment(Element.ALIGN_BOTTOM);
        } else {
            rtn.setVerticalAlignment(Element.ALIGN_MIDDLE);
        }

        return rtn;
    }

}
